package com.browsesoft.oa;

import java.io.File;
import java.util.Hashtable;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.browsesoft.PropertiesService;
import com.browsesoft.baseadapter.SimpleEntity;

/**
 * @author 李涛
 * @version 1.0
 */
public class CssStyle extends SimpleEntity {
	/**
	 * 
	 */
	private static final long serialVersionUID = 855121446464743415L;

	private BSMIMEParser parser;

	private HttpServletRequest requ;

	public CssStyle() {
		super();
	}

	public CssStyle(Hashtable attr) {
		super(attr);
	}

	/**
	 * 页面服务
	 * 
	 * @throws Exception
	 */
	public void service(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		super.service(request, response);
		this.requ = request;
		if (action != null && action.equals("confirm")) {
			parser = (BSMIMEParser) request.getAttribute("parser");
			if (parser != null) {
				this.attributes.put("realname", parser.getRealName());
			}
		}
	}

	/***************************************************************************
	 * 得到样式文件StyleSheetName
	 */
	public String getStyleSheetName() {
		return (String) this.getAttributes().get("f_stylesheetname");
	}

	public String toString() {
		return (String) this.getAttributes().get("title");
	}

	/**
	 * 更新属性
	 * 
	 * @param hashtable
	 *            属性集
	 * @throws Exception
	 */
	public void updateAttributes(Hashtable hashtable) throws Exception {
		// hashtable.remove("realname");
		this.getAttributes().putAll(hashtable);
		if (this.ifCssFile()) {
			super.updateAttributes(hashtable);
			// 如果是确定
			if (this.action != null && this.action.equals("confirm")) {
				String path = PropertiesService.getProperty("css", "path");
				// 把文件移到样式路径下
				BSHttpServletRequest.saveFile(this.requ, this
						.getStyleSheetName(), path);
			}
			this.BasicComponent.getPage().showMessage(
					"alert('样式文件上传成功！');window.close()");
		} else {
			// 如果不是样式文件，从attackment文件夹中删除该文件
			deleteTempFileInAttackement();
			this.BasicComponent.getPage().showMessage(
					"alert('您上传的样式文件的扩展名必须为.css！')");
		}
	}

	/**
	 * 当用户上传的不是样式文件是，从attackment文件中删除到刚上传的文件
	 * 
	 * @return
	 */
	private void deleteTempFileInAttackement() throws Exception {
		// 取得临时文件存放的路径
		String path = PropertiesService.getProperty("attackment", "path");
		// 得到文件名构造文件
		String name = this.getStyleSheetName();
		File file = new File(path + name);
		file.delete();
		this.deleteTempFile(file);
	}

	private void deleteTempFile(File file) {
		try {
			while (file.exists()) {
				file.delete();
				Thread.sleep(100);
			}
		} catch (InterruptedException ex) {
			return;
		}
	}

	/**
	 * 是否为样式文件，不是样式文件不能上传
	 */
	private boolean ifCssFile() {
		String fileName = this.getStyleSheetName();
		int length = fileName.length();
		int start = fileName.lastIndexOf(".");
		String expandName = fileName.substring(start, length);
		if (expandName != null && !expandName.equals(".css")) {
			this.BasicComponent.getPage().showMessage(
					"alert('您上传的样式文件的扩展名必须为.css！')");
			return false;
		}
		return true;
	}
}